<?php
/**
 * 函数类
 * 
 * @copyright  Copyright (c) 2012-2013 www.Mediad.cn
 * @license    New BSD License
*/
class Mediad2_Utility_Functions extends Mediad2_Utility_Abstract
{
	/**
	 * 从AUTH中获取id
	 *
	 * @param string $auth
	 * @return int $id
	 */
	public function getIdFromAuth($auth)
	{
		$authHelper = new Mediad2_Utility_AuthHelper();
		$auth = str_replace('@@@', '+', $auth);
		$auth = $authHelper->authcode($auth, 'DECODE', _VAL_KEY_);
		$info = explode('_@_', $auth);
		$id = $info[0];
		return $id;
	}
	
	/**
	 * 生成Auth
	 *
	 * @param int $id
	 * @param string $message
	 * @return string $auth
	 */
	public function createAuth($id, $message)
	{
		$validate = md5(md5(md5($message)));
		$validate_key = $id . '_@_'. $validate;
		$authHelper = new Mediad2_Utility_AuthHelper();
		$key = $authHelper->authcode($validate_key, 'ENCODE', _VAL_KEY_);
		$key = str_replace('+', '@@@', $key);
		return $key;
	}
	
	/**
	 * 生成UUID
	 * @return string uuid
	 */
	public function createGuid()
	{
		$charid = strtoupper(md5(uniqid(mt_rand(), true)));
		$hyphen = chr(45);// "-"
		$uuid = substr($charid, 0, 8).$hyphen
		.substr($charid, 8, 4).$hyphen
		.substr($charid,12, 4).$hyphen
		.substr($charid,16, 4).$hyphen
		.substr($charid,20,12)
		;
		return $uuid;
	}
	
	/**
	 * 根据UUID，生成IMEI
	 * @param string $guid
	 * @return string imei
	 */
	public function createImeiFromGuid($guid)
	{
		$str = str_replace('-', '', $guid);
		$str = substr($str,0,15);
		$arr = str_split($str);
		foreach($arr as $key=>$char) {
			$arr1[] = (int)((ord($char) & 0xFF) % 10);
		}
		$imei = implode('', $arr1);
	
		$len = strlen($imei);
		for($ii=1, $sum=0 ; $ii < $len ; $ii++){
			if($ii % 2 == 0) $prod = 2;
			else $prod = 1;
			$num = $prod * $imei[$ii-1];
			if($num > 9){
				$numstr = strval($num);
				$sum += $numstr[0] + $numstr[1];
			}else $sum += $num;
		}
	
		$strSum = strval($sum);
		$lenStrSum = strlen($strSum);
		$sumlast = intval($strSum[$lenStrSum-1]);
		$dif = (10-$sumlast);
		$diflast = intval(10*(($dif/10)-floor($dif/10))); 	//The last digit of $dif
	
		$imei = substr($imei,0,14) . $diflast;
		return $imei;
	}
	
	/**
	 * 生成IMEI
	 * @param int $num
	 * @return array
	 */
	function createImei($num=null)
	{
		$imeis = array();
		if (empty($num)) $num=10;
		for($i=1;$i<=$num;$i++) {
			$guid = $this->createGuid();
			$imei = $this->createImeiFromGuid($guid);
			$imeis[] = $imei;
		}
		return $imeis;
	}
	
	/**
	 * 获取qrcode
	 * @param int $in
	 * @return string $qrcode
	 */
	public function encodeQrcode($in,$length = 8)
	{
		$shorturl = new Mediad2_Utility_Shorturl();
		$qrcode = $shorturl->getShorturl($in, false, $length, _VAL_KEY_);
		return $qrcode;
	}
	
	/**
	 * 解析qrcode
	 * @param string $qrcode
	 */
	public function decodeQrcode($qrcode,$length = 8)
	{
		$shorturl = new Mediad2_Utility_Shorturl();
		$code = $shorturl->getShorturl($qrcode, true, $length, _VAL_KEY_);
		return $code;
	}
	
	/**
	 * array to xml
	 *
	 * @param array $array
	 * @return string $xml
	 */
	public function array2xml($array) {
		$xml = "";
		foreach ( $array as $key => $value ) {
			if (is_array ( $value )) {
				$xml .= "<$key>" . self::array2xml ( $value ) . "</$key>";
			} else {
				$xml .= "<$key>" . $value . "</$key>";
			}
		}
		return $xml;
	}
		
	/**
	 * @param string $type 生成内容类型 1：数字 2：字母 3：数字+字母
	 * @param int $len 需要字符的位数
	 */
	public function getVerifyCode($type,$len)
	{
		$numbers=array("0","1","2","3","4","5","6","7","8","9");
		$words=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
				"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
		
		$values=null;
		$count=null;
		switch ($type)
		{
			case "1":
				for ($n=0;$n<$len;$n++) {
					$count=count($numbers);
					$values=$values.$numbers[mt_rand(0, $count-1)];
				}
				break;
			case "2":
				for ($n=0;$n<$len;$n++) {
					$count=count($words);
					$values=$values.$words[mt_rand(0, $count-1)];
				}
				break;
			case "3":
				for ($n=0;$n<$len;$n++) {
					$keys=array_merge($numbers,$words);
					$count=count($keys);
					$values=$values.$keys[mt_rand(0, $count-1)];
				}
				break;
		}
		return $values;
	}
	
	/**
	 * base64解码方法
	 */
	public function decodeRequest($name)
	{
		if(empty($name)||!isset($name))return $name;
		$key = str_replace(" ", "+", $name);
		return base64_decode(str_replace('@@@', '+', $key));
	}
	
	/**
	 * 导入csv数据，并转换为数组
	 * @param resource $handle 文件指针
	 * @param int $length 行数，默认为10000
	 */
	public function input_csv($handle, $length=10000) 
	{
		$out = array ();     
		$n = 0;
		while(! feof($handle)) {
		    $data = fgetcsv($handle, 10000);
			$num = count($data);         
			for ($i = 0; $i < $num; $i++) {
				$data[$i] = iconv('gb2312', 'utf-8', $data[$i]);
				$out[$n][$i] = $data[$i];
			}
			$n++;
		}     
		return $out;
	}
	
}

